# Contributor: Celeste <cielesti@protonmail.com>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=mitra
pkgver=2.6.0
pkgrel=0
pkgdesc="ActivityPub microblogging platform written in Rust"
url="https://mitra.social/@mitra"
# riscv64: vite webapp fails to build: 'Parse error @:1:38'
arch="all !riscv64"
license="AGPL-3.0-only"
depends="postgresql"
makedepends="
	cargo
	cargo-auditable
	nodejs
	npm
	openssl-dev
	"
install="$pkgname.pre-install $pkgname.post-install"
pkgusers="mitra"
pkggroups="mitra"
subpackages="$pkgname-doc $pkgname-openrc"
source="mitra-$pkgver.tar.gz::https://codeberg.org/silverpill/mitra/archive/v$pkgver.tar.gz
	mitra-web-$pkgver.tar.gz::https://codeberg.org/silverpill/mitra-web/archive/v$pkgver.tar.gz
	mitra.initd
	init.sql
	config.yaml
	cargo-lock-version-update.patch
	"
builddir="$srcdir/mitra"

# use system openssl
export OPENSSL_NO_VENDOR=1

prepare() {
	default_prepare

	cargo fetch --target="$CTARGET" --locked

	cd "$srcdir/mitra-web"

	npm ci --foreground-scripts
}

build() {
	RUSTFLAGS="--cfg=ammonia_unstable" \
	cargo auditable build --frozen --release \
		--features production

	cd "$srcdir/mitra-web"

	echo 'VITE_BACKEND_URL=' > .env.local
	npm run build
}

check() {
	# These tests require a database connection
	RUSTFLAGS="--cfg=ammonia_unstable" \
	cargo test --frozen --workspace \
		--exclude mitra-models -- \
		--skip test_follow_unfollow \
		--skip test_hide_reblogs \
		--skip test_subscribe_unsubscribe \
		--skip test_get_jrd

	cd "$srcdir/mitra-web"

	npm run test
}

package() {
	install -Dm755 target/release/mitra -t "$pkgdir"/usr/bin
	install -Dm755 target/release/mitractl -t "$pkgdir"/usr/bin

	mkdir -p "$pkgdir"/usr/share/webapps
	cp -r "$srcdir"/mitra-web/dist \
		"$pkgdir"/usr/share/webapps/mitra

	install -Dm644 docs/* -t "$pkgdir"/usr/share/doc/$pkgname
	install -Dm644 config.yaml.example \
		contrib/Caddyfile contrib/*.nginx \
		contrib/monero/wallet.conf \
		-t "$pkgdir"/usr/share/doc/$pkgname/examples

	install -Dm640 -g mitra "$srcdir"/config.yaml -t "$pkgdir"/etc/mitra
	install -dm755 -o mitra -g mitra "$pkgdir"/var/lib/mitra
	install -Dm644 "$srcdir"/init.sql -t "$pkgdir"/var/lib/mitra
	install -Dm755 "$srcdir"/mitra.initd "$pkgdir"/etc/init.d/mitra
}

sha512sums="
1e21567b4fb4b78248542ae4ca61b1ef1c4f9b061c1c311c4761003238f97c194977c84c25d206707ba8860af7666bd7d6757e361780e60bf9651c81a6f446d8  mitra-2.6.0.tar.gz
8029f163e372ceba2147164ddcf7296026006e586b1282c6c232c0c99a504b1e9a3c91dc47334e00e3ad51816e6fb16bd298ac8e886ff350974795d58a6b1436  mitra-web-2.6.0.tar.gz
691f84f5dfdddc176e75792ab03ff167054246e75ced51be47a89f405ae55ebe5eb6280b73c1b467b5ecbe8539f6108fb3d86873d50fcc4f4b8c5b182632acb0  mitra.initd
180a47f5072534418b4aac3ce7c885a4f7e4dc38aca80d6d81c79848d12fbe24799788c3575bd195030a10da5e0372f87fa2809a4ef99a48eaa6df52f4d053dd  init.sql
6f1729954018f6a734673f8273a2ffe607721c7743505f95f0db26cf6d5a329a0dca8a88c3c21ad924ec76b81816ded1b67c524a2fe10eceb1fd742539870771  config.yaml
b12a2baa7e8e55fa98131202586ef9e657c6b4e7d29243f332470e0ceef243a8bcc97cc25395c316d3dc1777c2bdf610aade787f21c047de4ad95ec2d16f2691  cargo-lock-version-update.patch
"
